package com.trade.dao.impl;

import com.trade.dao.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.apache.commons.dbcp.BasicDataSource;
import org.hibernate.*;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.text.*;
import com.trade.data.*;
import com.trade.data.type.*;

import com.trade.data.calculation.InstrumentTechnicalCalValuesVO;
import com.trade.data.calculation.MACDValuesVO;
import com.trade.data.calculation.MovingAverageValuesVO;
import com.trade.data.calculation.RSIValuesVO;
import com.trade.data.calculation.BollingerBandValuesVO;

import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.compiler.PackageBuilderConfiguration;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
import org.drools.builder.conf.*;
import org.drools.runtime.rule.WorkingMemoryEntryPoint;
import org.drools.event.rule.DebugWorkingMemoryEventListener;
import org.drools.event.rule.DebugAgendaEventListener;
import org.drools.runtime.process.ProcessInstance;


//JFree

import javax.swing.JFrame;
import java.awt.BasicStroke;
import java.awt.Color;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.DateTickMarkPosition;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.DefaultHighLowDataset;
import org.jfree.chart.renderer.xy.CandlestickRenderer;
import org.jfree.chart.renderer.xy.HighLowRenderer;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
import org.jfree.data.xy.OHLCDataset;
import org.jfree.data.time.Day;
import org.jfree.data.time.ohlc.OHLCSeries;
import org.jfree.data.time.ohlc.OHLCSeriesCollection;


public class JFreeChartDemo extends ApplicationFrame{

	final DroolFlowDAO droolFlowDAO;

	public JFreeChartDemo( DroolFlowDAO droolFlowDAO ){

       	super("Chart");

		this.droolFlowDAO = droolFlowDAO;

	}


	 private JFreeChart createChart(final OHLCDataset  dataset) {
			final JFreeChart chart = ChartFactory.createCandlestickChart(
				"Candlestick Demo",
				"Time",
				"Value",
				dataset,
				true
			);
			return chart;
	}

	public void draw30MinChart(String symbol , IntervalTimeTypes intervalTimeTypes){

		try{

			OHLCSeriesCollection dataset = new OHLCSeriesCollection();

			OHLCSeries s1 = new OHLCSeries(symbol +" Chart");


			List<EquityIntradayDataVO> equityList = (List<EquityIntradayDataVO>)droolFlowDAO.getEquityIntradayData(new Object[]{ symbol , intervalTimeTypes } , intervalTimeTypes );

			int i =1 , j=1 , k=2000;

			int m = 0;

			for(EquityIntradayDataVO equity : equityList){

				//add(RegularTimePeriod period, double open, double high, double low, double close)
				System.out.println(i+"/"+j+"/"+k);
				s1.add(new Day(i, j, k), equity.getOpen(), equity.getHigh(), equity.getLow(), equity.getClose());
				i++;
				if( i == 28 ){
					i = 1;
					j++;
				}
				if( j == 12 ){
					j = 1;
					k++;
				}
			}

			dataset.addSeries(s1);

			//final DefaultHighLowDataset dataset = DemoDatasetFactory.createHighLowDataset();
			final JFreeChart chart = createChart(dataset);
			chart.getXYPlot().setOrientation(PlotOrientation.VERTICAL);

			 NumberAxis axis = (NumberAxis) chart.getXYPlot().getRangeAxis();
			 axis.setAutoRangeIncludesZero(false);
			 axis.setUpperMargin(0.0);
			 axis.setLowerMargin(0.0);

			DateAxis dateAxis = (DateAxis) chart.getXYPlot().getDomainAxis();
	        dateAxis.setTickMarkPosition(DateTickMarkPosition.MIDDLE);

			CandlestickRenderer renderer = (CandlestickRenderer) chart.getXYPlot().getRenderer();
	        renderer.setBaseStroke(new BasicStroke(3.0f));
	        renderer.setSeriesPaint(0, Color.blue);

			final ChartPanel chartPanel = new ChartPanel(chart);
			chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
			setContentPane(chartPanel);
		}catch(Exception e){
			System.out.println(""+e.getMessage());
		}

	}
}